#!/bin/bash
set -e
#****************************************************************#
# 一键安装k8s1.14版本，自动生成node安装脚本
# 适用于centos7.0+版本
#****************************************************************#
#1.配置阿里云kubernetesyum源
#配置文件位置
touch /etc/yum.repos.d/kubernetes.repo
tee /etc/yum.repos.d/kubernetes.repo <<-'EOF'
[kubernetes]
name=Kubernetes Repositoryu
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
EOF
#配置docker源
yum install -y yum-utils
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum-config-manager --add-repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum-config-manager --add-repo http://mirrors.aliyun.com/repo/epel-7.repo
yum makecache fast
#2.yum安装kubeadm和相关工具
yum install -y kubelet-1.14.0 kubeadm-1.14.0 kubectl-1.14.0 --disableexcludes=kubernetes
systemctl enable kubelet && systemctl start kubelet
#kubeadm reset #adm初始化
yum install -y docker-ce-18.06.3.ce-3.el7
yum -y install gcc net-tools wget curl unzip zip dos2unix python-setuptools bash-completion-extras bash-completion rsync
yum -y install device-mapper-persistent-data lvm2 deltarpm
systemctl enable docker && systemctl start docker
#4.关闭防火墙并配置其他配置
setenforce 0
systemctl stop firewalld
systemctl disable firewalld
sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
#关闭交换分区
swapoff -a && sed -i "s/\/dev\/mapper\/centos-swap/\#\/dev\/mapper\/centos-swap/g" /etc/fstab
echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables
#配置docker加速以及启动方式
mkdir -p /etc/docker
touch /etc/docker/daemon.json
tee /etc/docker/daemon.json <<-'EOF'
{
 "registry-mirrors": ["https://1e60esib.mirror.aliyuncs.com"],
 "exec-opts": ["native.cgroupdriver=systemd"]
}
EOF
systemctl daemon-reload
systemctl restart docker
#5.配置kubeadm
#初始化文件
touch init-config.yaml
tee init-config.yaml <<-'EOF'
apiVersion: kubeadm.k8s.io/v1beta1
kind: ClusterConfiguration
imageRepository: docker.io/dustise
kubernetesVersion: v1.14.0
networking:
    podSubnet: "192.168.0.0/16"
EOF
#pull镜像
kubeadm config images pull --config=init-config.yaml
#初始化master
#join=$(kubeadm init --config=init-config.yaml | egrep "(kubeadm join|discovery-token-ca-cert-hash)")
kubeadm init --config=init-config.yaml
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
#安装网络插件
kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')"
echo "稍等两分钟运行‘kubectl get nodes’查看master节点状态’"